/*
 * Pyx4j framework
 * Copyright (C) 2008-2011 pyx4j.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * Created on Oct 2, 2011
 * @author vlads
 * @version $Id: code-templates.xml 7812 2011-01-10 20:13:00Z vlads $
 */
package org.hsqldb.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UpdateRow {

    public static void main(String[] args) throws Exception {
        Connection con;

        boolean testHSQL = true;

        if (testHSQL) {
            Class.forName("org.hsqldb.jdbcDriver");
            con = DriverManager.getConnection("jdbc:hsqldb:mem", "sa", "");
            con.createStatement()
                    .executeUpdate(
                            "CREATE TABLE test(name varchar(500))");
        } else {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost/tst_entity", "tst_entity", "tst_entity");
            //con.createStatement().executeUpdate(
            //        "CREATE TABLE test(id bigint(20) NOT NULL AUTO_INCREMENT, name varchar(500),  owner bigint(20), CONSTRAINT TEST_PK PRIMARY KEY (id))");
        }

        //Setup test data
        con.createStatement().executeUpdate("INSERT INTO test (name)  VALUES ('1')");
        con.createStatement().executeUpdate("INSERT INTO test (name)  VALUES ('2')");

        // Test itself
        String sql = "SELECT name FROM test ";
        PreparedStatement stmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery();

        int cnt = 10;
        while (rs.next()) {
            rs.updateString("name", "u" + String.valueOf(cnt++));
            rs.updateRow();
        	System.out.println(rs.getString(1));
        }
        //con.commit();
    }
}
